package edu.tec.mty.test.gui;

import java.util.HashMap;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Menu {
	
	private JMenu menu;
	private String name;
	private HashMap<String, JMenuItem> menuItems;
	private Font font;
	
	public Menu(JMenu menu, String name) {
		this.menuItems = new HashMap<String, JMenuItem>();
		this.menu = menu;
		this.name = name;
		this.font = new Font("Arial", 0, 11);
	}
	
	public void addMenuItem(String name, ActionListener al) {
		JMenuItem menuItem = new JMenuItem(name);
		menuItem.setFont(this.font);
		menuItem.setActionCommand(this.name + "." + name);
		menuItem.addActionListener(al);
		this.menuItems.put(name, menuItem);
		this.menu.add(menuItem);
	}
	
	public void activate(String menuItem) throws Exception {
		if (this.menuItems.containsKey(menuItem)) {
			this.menuItems.get(menuItem).setEnabled(true);
		} else {
			throw new Exception("Item doesnt exist");
		}
	}
	
	public void deactivate(String menuItem) throws Exception {
		if (this.menuItems.containsKey(menuItem)) {
			this.menuItems.get(menuItem).setEnabled(false);
		} else {
			throw new Exception("Item doesnt exist");
		}
	}
	
}

public class Menus {

	private HashMap<String, Menu> menus;
	private JMenuBar menuBar;
	private Font font;
	
	public Menus() {
		this.menus = new HashMap<String, Menu>();
		this.menuBar = new JMenuBar();
		this.font = new Font("Arial", 1, 11);
	}
	
	public void addMenu(String name) {
		JMenu menu = new JMenu(name);
		menu.setFont(this.font);
		menus.put(name, new Menu(menu, name));
		this.menuBar.add(menu);
	}
	
	public void addItem(String menu, String name, ActionListener al) throws Exception {
		if (this.menus.containsKey(menu)) {
			this.menus.get(menu).addMenuItem(name, al);
		} else {
			throw new Exception("The menu doesnt exist");
		}
	}
	
	public void activateMenuItem(String menu, String menuItem) throws Exception {
		if (this.menus.containsKey(menu)) {
			this.menus.get(menu).activate(menuItem);
		} else {
			throw new Exception("Menu doesnt exist");
		}
	}
	
	public void deactivateMenuItem(String menu, String menuItem) throws Exception {
		if (this.menus.containsKey(menu)) {
			this.menus.get(menu).deactivate(menuItem);
		} else {
			throw new Exception("Menu doesnt exist");
		}
	}
	
	public JMenuBar getMenuBar() {
		return this.menuBar;
	}
	
}
